package com.qf.administration.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.administration.entity.ConferenceSort;
import com.qf.administration.mapper.ConferenceSortMapper;
import com.qf.administration.qo.SortQO;
import com.qf.administration.service.SortService;
import com.qf.administration.utils.CommonBeanUtils;
import com.qf.administration.vo.SortVO;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @Title: SortServiceImpl
 * @Description:
 * @author:ZWX
 * @date: 2023/5/23 16:02
 */
@Service
public class SortServiceImpl implements SortService {
    @Resource
    private ConferenceSortMapper conferenceSortMapper;

    @Override
    public RespResult<String> addSort(SortQO sortQO) {
        ConferenceSort sort = conferenceSortMapper.selectById(sortQO.getSortId());
        if (ObjectUtils.isEmpty(sort)) {
            ConferenceSort conferenceSort = new ConferenceSort();
            CommonBeanUtils.copyProperties(sortQO, conferenceSort);
            int insert = conferenceSortMapper.insert(conferenceSort);
            if (insert > 0) {
                return RespResult.success("添加成功");
            } else {
                throw new ServiceException(ResultCode.INSERT_ERROR);
            }
        } else {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }

    }

    @Override
    public RespResult<String> deleteSort(int[] ids) {
        int i = conferenceSortMapper.deleteBySortId(ids);
        if (i != ids.length) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
        return RespResult.success("删除成功");
    }

    @Override
    public RespResult<String> updateSort(SortQO sortQO) {
        ConferenceSort conferenceSort = new ConferenceSort();
        BeanUtils.copyProperties(sortQO, conferenceSort);
        int i = conferenceSortMapper.updateById(conferenceSort);
        if (i < 1) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
        return RespResult.success("更新成功");
    }

    @Override
    public PageInfo<SortVO> querySort(int page, int size, String sortName) {
        PageInfo<ConferenceSort> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> conferenceSortMapper.selectList(sortName));
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return CommonBeanUtils.copyPageInfo(pageInfo, new PageInfo<>(), SortVO::new);
    }

    @Override
    public SortVO querySortOne(int sortId) {
        ConferenceSort conferenceSort = conferenceSortMapper.selectById(sortId);
        if (ObjectUtils.isEmpty(conferenceSort)) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        } else {
            SortVO sortVO = new SortVO();
            BeanUtils.copyProperties(conferenceSort, sortVO);
            return sortVO;
        }
    }

}
